/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.HotelsBusiness;
import business.PlacesBusiness;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Hotels;
import model.Places;

/**
 *
 * @author Administrator
 */
@ManagedBean
@RequestScoped
public class MngHotelBean {

    private HotelsBusiness hotelsBusiness = new HotelsBusiness();
    private PlacesBusiness placesBusiness = new PlacesBusiness();
    private Hotels hotel;
    private List<Places> listPlaces;
    private List<Hotels> listHotel;
    private int pageID;
    private List<Integer> pageList;

    public int getPageID() {
        return pageID;
    }

    public void setPageID(int pageID) {
        this.pageID = pageID;
    }

    public Hotels getHotel() {
        return hotel;
    }

    public void setHotel(Hotels hotel) {
        this.hotel = hotel;
    }

    public List<Places> getListPlaces() {
        return listPlaces;
    }

    public void setListPlaces(List<Places> listPlaces) {
        this.listPlaces = listPlaces;
    }

    public List<Hotels> getListHotel() {
        return listHotel;
    }

    public void setListHotel(List<Hotels> listHotel) {
        this.listHotel = listHotel;
    }

    public List<Integer> getPageList() {
        return pageList;
    }

    public void setPageList(List<Integer> pageList) {
        this.pageList = pageList;
    }

    public String updateHotelModelAction(Hotels hotels) {
        hotelsBusiness.updateHotels(hotels);
        return "managementhotels.xhtml";
    }

    public String addNewHotelAction() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (session.getAttribute("sessionFile3") != null) {
            String path = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sessionFile3").toString();
            String getfile = "images/upload/" + path;
            hotel.setImages(getfile);
            hotelsBusiness.newHotels(hotel);
        }
//        listHotel = hotelsBusiness.listAllHotelDetail();
        return "managementhotels.xhtml";
    }

    /** Creates a new instance of MngHotelBean */
    public MngHotelBean() {
        int pageIndex = hotelsBusiness.getRowInHotel();
        pageList = new ArrayList<Integer>();
        if (pageIndex % 3 == 0) {
            for (int i = 1; i <= pageIndex / 3; i++) {
                pageList.add(i);
            }
        } else {
            for (int i = 1; i <= pageIndex / 3 + 1; i++) {
                pageList.add(i);
            }
        }
        hotel = new Hotels();
//        listHotel = hotelsBusiness.listAllHotelDetail();
//        listPlaces = placesBusiness.listAllPlaces();
        if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID") != null) {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID").toString());
        } else {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("PageID").toString());
        }
        listHotel = hotelsBusiness.listHotelByPage(pageID);
        listPlaces = placesBusiness.listAllPlaces();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("PageID", pageID);
    }
}
